package com.itrip.biz.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itrip.beans.pojo.Comment;
import com.itrip.beans.vo.CommentCountVo;
import com.itrip.beans.vo.CommentListVo;
import com.itrip.beans.vo.HotelScoreVo;
import com.itrip.beans.vo.PageVo;
import com.itrip.biz.service.CommentService;
import com.itrip.dao.CommentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author 周勉
 * @since 2020-02-28
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Resource
    private CommentMapper commentMapper;
    @Override
    public HotelScoreVo getHotelScore(Integer hotelId) {
        return commentMapper.getHotelScore(hotelId);
    }

    @Override
    public CommentCountVo getCount(Integer hotelId) {
        //根据ld获取所有评论
        QueryWrapper qw=new QueryWrapper();
        qw.eq("hotel_id",hotelId);
        List<Comment> commentList=commentMapper.selectList(qw);
        CommentCountVo count=new CommentCountVo();
        for (Comment comment:commentList) {
            count.setAllcomment(count.getAllcomment()+1);
            if(comment.getIsHavingImg()==1){
                count.setHavingimg(count.getHavingimg()+1);
            }
            if (comment.getIsOk()==1){
                count.setIsok(count.getIsok()+1);
            }else{
                count.setImprove(count.getImprove()+1);
            }
        }
        return count;
    }

    @Override
    public PageVo<Comment> getCommentList(CommentListVo vo) {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("hotel_id",vo.getHotelId());
        if(vo.getIsOk()==1){
            qw.eq("is_ok",1);
        }else if(vo.getIsOk()==0){
            qw.eq("is_ok",0);
        }
        if(vo.getIsHavingImg()==1){
            qw.eq("is_having_img",1);
        }
        Page<Comment> page = new Page<Comment>(vo.getPageNo(),vo.getPageSize());
        IPage<Comment> orderF= commentMapper.selectPage(page,qw);
        PageVo<Comment> pageVo=new PageVo<Comment>(vo.getPageNo(),vo.getPageSize(),(int)orderF.getTotal());
        pageVo.setRows(orderF.getRecords());
        return pageVo;
    }


    @Override
    public Comment getLastComment() {
        return commentMapper.getLastComment();
    }

}
